import 'package:common/common.dart';

typedef LogoutChecker = bool Function();

class TokenCheckingInterceptor extends Interceptor {
  static LogoutChecker checker;

  @override
  Future onResponse(Response response) {
    bool canLogout = true;
    if (checker != null) {
      canLogout = checker();
    }
    if (canLogout &&
        response != null &&
        response.data != null &&
        (response.data is Map) &&
        response.data["code"] != null) {
      int code = response.data["code"];
      print(
          "TDio: /core/getInfo == response.request.path: ${("/tcore/getInfo" == response.request.path)}");
      if (code == 303 ||
          (code == 401 && ("/tcore/getInfo" == response.request.path))) {
        print('tDio: token失效 请求来源：${response.request.path}');
        GlobalBlocs.get(UserBloc.blocName).add(UserLogoutEvent());
      }
    }
    return Future.value(response);
  }
}
